package com.ant.storage.controller;

import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.CommodityDto;
import com.ant.storage.dto.InventoryCheckDto;
import com.ant.storage.pojo.Commodity;
import com.ant.storage.service.ICommodityService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/commodity")
@Api(tags = "商品信息接口")
public class CommodityController {

    @Autowired
    private ICommodityService commodityService;

    @ApiOperation(value = "获取商品分页信息")
    @GetMapping("/getCommodityPage")
    public ResultPage getCommodityPage(CommodityDto dto) {
        return ResultPage.success("操作成功", commodityService.getCommodityPage(dto));
    }

    @ApiOperation(value = "获取商品列表信息")
    @GetMapping("/getCommodityList")
    public ResultPage getCommodityList(CommodityDto dto){
        return ResultPage.success("操作成功",commodityService.listPage(dto));
    }

    @ApiOperation(value = "保存信息")
    @PostMapping("/saveCommodity")
    public ResultVo saveCommodity(@RequestBody @Valid CommodityDto commodityDto, BindingResult bindingResult){
        return  commodityService.saveCommodity(commodityDto,bindingResult);
    }

    @ApiOperation(value = "查询商品库存等详细信息")
    @GetMapping("/queryCommodity")
    public ResultVo queryCommodity(Integer commodity_id,Integer warehouse_id){
        return ResultVo.success("操作成功",commodityService.queryCommodity(commodity_id,warehouse_id));
    }

    @ApiOperation(value = "查询商品库存等详细信息列表")
    @PostMapping("/queryCommodityList")
    public ResultPage queryCommodityList(PageRecv<InventoryCheckDto>pageRecv){
        return commodityService.queryCommodityList(pageRecv);
    }


    @ApiOperation(value = "上传商品")
    @PostMapping("/uploadCommodity")
    public ResultVo uploadCommodity(@RequestParam("file") MultipartFile file){
        return commodityService.uploadCommodity(file);
    }
    @ApiOperation(value = "删除")
    @PutMapping("/del/{id}")
    public ResultVo delPickingById(@PathVariable("id") Integer id) {
        commodityService.update(new UpdateWrapper<Commodity>().set("is_delete", 0).eq("commodity_id", id));
        return ResultVo.success("操作成功");
    }


}
